var pro_load_page = 2,pro_load_flag = true;
var orderlist ={
	init : function(page,status){
		console.log('sta'+status);
		app.ajax(
		'/Member/Index/order_list',
			{
				page :page,
				status : status
			},function(res){
				if(res.code == 200){
					if(res.list.length>0){
					app.tpl($('.order-list1'),'tpl-order-list1',res);
					}else{
						$('.order-list1').append("<div class='tC promp-order' style='position:absolute;top:50%;left:15%;height:30%;width:70%;'><span class='cGrey fS15'>亲  这个页面还没有数据哦~</span></div>");
					}
				}else{
					layer.msg('网络繁忙，请稍后再试');
				}
			}
		);
	},
	loadMore :function(url,data){
		var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;" class="no-more">努力加载中~<div>';
   		$('.order-list1').append(listLoadingEmpty);
		pro_load_flag = false;
		app.ajax(
			url,
			data,
			function(res){
				$('.order-list1 .no-more').remove();
				if(res.code == 200){
						app.tpladd($('.order-list1'),'tpl-order-list1',res);						
						pro_load_page++;
						pro_load_flag = true;
					//page++;
				}else{						
						var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;">没有更多数据了~<div>';
						$('.order-list1').append(listLoadingEmpty);
				}
			});	
	}
}
orderlist.init(1,5);
footer.init();
header.init('订单列表');
window.sessionStorage.setItem('order_status',0);

window.onload = function(){
	$('.nav-ul li:first i').show();
	
	$('body').on('click','.nav-ul li',function(){
		$('.nav-ul li h2').removeClass('font-active');
		$('.nav-ul li i').hide();
		
		$(this).find('h2').addClass('font-active');
		$(this).find('i').show();
		var status = $(this).attr('data-status');
		console.log(status);
		window.sessionStorage.setItem('order_status',status);
		console.log(status);
		$('.order-list1 .promp-order').remove();
		$('.order-list1').empty();
		window.scrollTo(0,0);
		orderlist.init(1,status);
	});
	
	if($('header').html()){
		$('.explain').css('marginTop','7.34rem');
	}
	
	$('body').on('click','.order-list1 .list-o',function(){
		var ids = $(this).attr('data-id');
		app.open('user.orderDetail.html','orderDetail',{id:ids},window.location.href,document.body.scrollTop);
		return false;
	});
	$('body').on('click','.order-status .cancel',function(){
		var ids = $(this).attr('data-id');
		var t = $(this);
		layer.open({
			content:'确定要取消订单吗？',
			btn:['确定','取消'],
			shadeClose:false,
			yes:function(index){
				layer.close(index);
				app.ajax(
					'/Member/Index/order_clear',
					{
						order_id : ids
					},function(res){
						if(res.code == 200){
							layer.msg('取消订单成功');
							history.go(-1);
						}else{
							layer.msg(res.msg);
						}
					}
				);
			}
		});
		return false;
		
	});
	
	$('body').on('click','.order-status .topay',function(){
		var ids = $(this).attr('data-id'),
		type = 1;
		
		var ua = window.navigator.userAgent.toLowerCase();
		if(ua.match(/MicroMessenger/i) != 'micromessenger'){
			type = 1;
		}else{
			type = 2;
		}

		app.ajax(
			'/Member/Index/order_pay',
			{
				order_id : ids,
				payTypeId : type
			},function(res){
				if(res.code ==200){
					$('body').html(res.html);
				}else{
					layer.msg('网络忙请稍候再试');
				}
			}
		);
		return false;
	});
	
	$('body').on('click','.wuliu',function(){
		var kname =$(this).attr('data-kd');
		var ids = $(this).attr('data-id');
		app.open('shop.viewLogistic.html','shop_viewLogistic',{id : ids,kdn:kname},window.location.href,document.body.scrollTop);
	});
	
	$(window).scroll(function(){
		// 当滚动到距底部以上200像素
		var offset = $(".order-list1").offset(),
		dataType = 0,
		data = {};
		var documentHeight = offset.top + $(".order-list1").height();
		if (documentHeight - $(this).scrollTop() - $(this).height()<100){
				if(window.sessionStorage.getItem('order_status')){
				 dataType=window.sessionStorage.getItem('order_status');
				}
				data = {
					page :pro_load_page,
					status : dataType
				};
			pro_load_flag && orderlist.loadMore('/Member/Index/order_list',data);
		}
	});
};
